/*
  2009-06-19 :
   1. 增加 getFile by file name 的方法。
   2. 增加 getInputStream by file name 的方法。
   3. 增加 contains 的方法。

  2009-04-01 :
   1. 增加 getFile 的方法。
   2. 增加 deleteAll 的方法。

  2009-04-02 :
   1. 獨立保存以檔案名稱為 key 的 ArrayList 物件，並確實對 keys 執行 Collection.sort 的排序。

ASR 指令 :
rem START "GCDemo" CMD /K bin\GcDemoDMXML -gconfig\DMVOutBound.cfg -ischedule.cfg -xdz_dctl_list.txt -dlist\ASROBList.txt -f1 -l2 -oASROBList-0402-1
rem START "GCDemo" CMD /K bin\GcDemoDMXML -gconfig\DMVOutBound.cfg -ischedule.cfg -xdz_dctl_list.txt -dlist\ASROBList-0402-1.off -f1 -l2 -oASROBList-0402-2

bin\GcDemoDMXML -gconfig\DMVOutBound.cfg -ischedule.cfg -xdz_dctl_list.txt -dlist\ASROBList.txt -f1 -l2 -oASROBList-0402-1
bin\GcDemoDMXML -gconfig\DMVOutBound.cfg -ischedule.cfg -xdz_dctl_list.txt -dlist\ASROBList-0402-1.off -f1 -l2 -oASROBList-0402-2

REM bin\GcDemoDMXML -gconfig\DMVOutBound.cfg -ischedule.cfg -xdz_dctl_list.txt -dlist\test.txt -f1 -l2 -o0402_1
REM bin\GcDemoDMXML -gconfig\DMVOutBound.cfg -ischedule.cfg -xdz_dctl_list.txt -d0402_1.off -f1 -l2 -o0402_2


可開啟記憶功能，用滾動重複存取資料的概念下去做。
 可開啟 FTP 的檔案
 掃描內層的檔案 /s *.txt
*/
package idv.jieh.commons.io;

import java.io.*;
import java.util.*;

import idv.jieh.commons.util.*;

public class OpenFiles extends idv.jieh.commons.comm.BasicClass{

  private LinkedHashMap <String, File>files = null;
  private LineNumberReader current_lnr = null;
  private ArrayList files_keys = null;

  public int load(String path, String search_name){
    files = new LinkedHashMap();
  // -1 = any file, 0 = no star, 1 = *.txt, 2 = test.*, 3 = *test*
    int where_star = search_name.equals("*")||search_name.equals("*.*")
                     ?-1:search_name.indexOf("*")==-1
                     ?0:search_name.startsWith("*")
                     ?search_name.endsWith("*")
                     ?3:1:2;
    search_name = DataAnalysis.replace(search_name, "*", "");

    File open_file = new File(path);
    File[] file_list = open_file.listFiles();
    int r = 0;
    String file_name;
    for (int i=0, l=file_list.length; i<l; i++){
      if(file_list[i].isFile()){
        file_name = file_list[i].getName();
        if (inSearch(where_star, file_name, search_name)) {
          r++;
          files.put(file_name, file_list[i]);
        }
      }
    }

    files_keys = new ArrayList(files.keySet());
    Collections.sort(files_keys);

    return r;
  }

  private boolean inSearch(int where_star, String name, String search_name){
    switch(where_star){
      case -1: return true;
      case 0: return name.equals(search_name);
      case 1: return name.endsWith(search_name);
      case 2: return name.startsWith(search_name);
      case 3: return name.indexOf(search_name) != -1;
      default: return false;
    }
  }

  public boolean contains(String filename){
    return files_keys.contains(filename);
  }

  /**
   * 依檔案名稱取得檔案。
   * @param filename String
   * @return File
   */
  public File getFile(String filename){
    return files.get(filename);
  }

  /**
   * 依檔案名稱取得檔案資料流。
   * @param idx int
   * @return InputStream
   * @throws IOException
   */
  public InputStream getInputStream(String filename) throws IOException{
    return new FileInputStream(getFile(filename));
  }

  /**
   * 循序取得檔案。
   * @param idx int
   * @return File
   */
  public File getFile(int idx){
    return files.get(files_keys.get(idx));
  }

  /**
   * 循序取得檔案資料流。
   * @param idx int
   * @return InputStream
   * @throws IOException
   */
  public InputStream getInputStream(int idx) throws IOException{
    return new FileInputStream(getFile(idx));
  }

  /**
   * 刪除全部檔案。
   * @return int
   */
  public int deleteAll(){
    int r = 0;
    for(File f : files.values()){
      r += f.delete()?1:0;
    }
    return r;
  }


//******** 循序開檔讀取檔案內容 開始 ********//

  /**
   * @param idx int
   * @return boolean
   * @throws IOException
   */
  public void open(int idx) throws IOException{
    current_lnr = new LineNumberReader(new BufferedReader(new FileReader(getFile(idx))));
  }

  /**
   * @return boolean
   * @throws IOException
   */
  public boolean ready() throws IOException{
    return current_lnr.ready();
  }

  /**
   * @return String
   * @throws IOException
   */
  public String readLine() throws IOException{
    return current_lnr.readLine();
  }

  /**
   * @param delim String
   * @return ArrayList
   * @throws IOException
   */
  public ArrayList readLineToArray(String delim) throws IOException{
    StringTokenizer st = new StringTokenizer(readLine(), delim);
    ArrayList <String>r = new ArrayList();
    while(st.hasMoreElements()){
      r.add(st.nextToken());
    }
    return r;
  }

  /**
   * @throws IOException
   */
  public void close() throws IOException{
    current_lnr.close();
  }

//******** 循序開檔讀取檔案內容 結束 ********//

  public void print(){
    for(int i=0, l=files_keys.size(); i<l; i++){
      System.out.println(i + ". " + files_keys.get(i));
    }
  }


  public static void main(String[] args) throws IOException{
    OpenFiles me = new OpenFiles();
    me.load("./", "*.*");
//    me.load("D:/project/tcbank/ASR/test/", "*.out");
    me.print();
  }

}



/**
 * @deprecated 開發測試中勿使用！
 * @throws IOException
 */
/*
public void make() throws IOException{
File f = new File("y:/CashCard.txt");
  FileWriter fw = new FileWriter(f);
  BufferedWriter bw = new BufferedWriter(fw);

  for(File of : files.values()){
    LineNumberReader lnr = new LineNumberReader(new BufferedReader(new FileReader(of)));
    while(lnr.ready()){
      Vector v = DataAnalysis.string2vector(lnr.readLine());
      // v.contains("Z34") || v.contains("Z35") || v.contains("Z71") || <= 接通其他
      if(v.contains("Z70") || v.contains("Z13") || v.contains("Z14")){
        bw.write(v.get(0) + "," + v.get(1) + "," + v.get(7) + ",0320");
        bw.newLine();
      }
    }
  }
  bw.flush();
  bw.close();
  }
*/
